﻿using System;
using System.Collections.Generic;
using System.Configuration.Provider;
using System.Linq;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.Web.SessionState;
using Salient.Web.Security;
using Salient.Web.SQLite.Model;

namespace SQLiteProviderStack
{
    public class Global : System.Web.HttpApplication
    {

        public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args)
        {
            ProfileBase anonymousProfile = ProfileBase.Create(args.AnonymousID, false);

            string userName = User.Identity.Name;
            if (ProfileManager.FindProfilesByUserName(ProfileAuthenticationOption.Authenticated, userName).Count == 0)
            {
                ProfileBase profile = ProfileBase.Create(userName, true);
                profile["Counter"] = anonymousProfile["Counter"];
                profile["DisplayName"] = userName;
                profile.Save();

            }

            ProfileManager.DeleteProfile(args.AnonymousID);
            AnonymousIdentificationModule.ClearAnonymousIdentifier();
            Membership.DeleteUser(args.AnonymousID, true);

        }
        protected void Application_Start(object sender, EventArgs e)
        {
            try
            {
                if (!Roles.RoleExists("administrators"))
                {
                    MembershipCreateStatus status;
                    Membership.CreateUser("administrator", "@dministr8or", "foo@testuri.org", "mu", "purple", true, null,
                                          out status);

                    if (status != MembershipCreateStatus.Success)
                    {
                        throw new Exception("Could not initialize Membership database");
                    }

                    Roles.CreateRole("administrators");
                    Roles.AddUserToRole("administrator", "administrators");

                    BuildSiteMap();
                }

            }
            catch (Exception ex)
            {

                throw new Exception("An exception occured while initializing default site data.", ex);
            }


        }

        private static void BuildSiteMap()
        {
            List<string> administratorsRoles = new List<string> { "administrators" };



            SQLiteSiteMapProvider sitemap = (SQLiteSiteMapProvider)SiteMap.Provider;

            var rootNode = new SiteMapNode(SiteMap.Provider, "root", "~/default.aspx", "Home", "This is the Home Page", null, null, null, null);
            sitemap.Add(rootNode, null);
            sitemap.Add(new SiteMapNode(SiteMap.Provider, "pub-content", "~/pub/SomePublicContent.aspx", "Public Content", "This is some public content", null, null, null, null), rootNode);

            SiteMapNode adminRoot = new SiteMapNode(SiteMap.Provider, "admin-root", "~/admin/default.aspx", "Admin", "This is the Admin Page", administratorsRoles, null, null, null);
            sitemap.Add(adminRoot, rootNode);
            sitemap.Add(new SiteMapNode(SiteMap.Provider, "admin-health", "~/admin/HealthMonitoring.aspx", "Health Monitoring", "Configure health monitoring", administratorsRoles, null, null, null), adminRoot);


        }

        protected void Session_Start(object sender, EventArgs e)
        {
            // custom events must use eventCode>100000
            SimpleSampleWebBaseEvent evt = new SimpleSampleWebBaseEvent("Session Started:" + Session.SessionID, this, 100001);
            evt.Raise();
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {


        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}